package com.benyanyi.image_grid_lib;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

/**
 * @author mylove
 * @date 2021/9/23
 * @email ben@yanyi.red
 * @overview
 */
public class SquareRelativeLayout extends RelativeLayout {
    public SquareRelativeLayout(Context context) {
        super(context);
    }

    public SquareRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public SquareRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMeasure = widthMeasureSpec;
        int heightMeasure = heightMeasureSpec;
        setMeasuredDimension(View.getDefaultSize(0, widthMeasure), View.getDefaultSize(0, heightMeasure));
        widthMeasure = MeasureSpec.makeMeasureSpec(widthMeasure, MeasureSpec.EXACTLY);
        heightMeasure = MeasureSpec.makeMeasureSpec(heightMeasure, MeasureSpec.EXACTLY);
        if (widthMeasure > heightMeasure) {
            widthMeasure = heightMeasure;
        } else {
            heightMeasure = widthMeasure;
        }
        super.onMeasure(widthMeasure, heightMeasure);
    }
}
